﻿using System;
using System.Linq;
using System.Linq.Expressions;
using System.Linq.Dynamic;using System.Text;
using System.Data;
using System.Data.Linq;
using System.Transactions;
using LinqToSqlExtensions;using System.ServiceModel;
using System.Collections;
using System.Collections.Generic;
using CardsServer.DAL;
using CardsServer.Tools;
namespace CardsServer.WCF
{
    public partial interface IService
    {




        [OperationContract]
        string phoneCZ(CZ_M m,List<CZ_D> ds,int ACID);
    }
    public partial class Service : IService
    {


        public string phoneCZ(CZ_M m, List<CZ_D> ds, int ACID)
        { 
            string r = "";
            using (TransactionScope tc = new TransactionScope())
            {
                try
                {
                    using (CardsServer.DAL.DataClassesDataContext ctx = new CardsServer.DAL.DataClassesDataContext(getAccConnStr(ACID)))
                    {
                        string r1 = CZ_M_Add(m, ACID, ctx);
                        if (!string.IsNullOrEmpty(r1))
                        {
                            throw new Exception(r1);
                        }
                        
                        
                        foreach (CZ_D d in ds)
                        {
                            d.pid = m.pid;
                            string r2 = CZ_D_Add(d, ACID, ctx);

                            if (!string.IsNullOrEmpty(r2))
                            {
                                throw new Exception(r2);
                            }

                            numberpool phone = ctx.numberpool.Single(p => p.phone == d.phone);

                            RecordBalance(m.total, m.ClientID, ctx, ACID);
                        }
                        ctx.SubmitChanges();
                        tc.Complete();
                    }
                }
                catch (Exception ex)
                {
                    r = ex.Message;
                }
            }
            return r;
        }


    }


}

